package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApCollectionService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.dtos.CollectionBehaviorDto;
import com.heima.model.user.pojos.ApCollection;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionServiceimpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Override
    /**
     * 收藏
     *
     * @param dto
     * @return
     */
    public ResponseResult collection(CollectionBehaviorDto dto) {
        Integer id = AppThreadLocalUtil.getUser().getId();
        if (dto.getEntryId() == null || dto.getOperation() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApCollection collection = getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, id).eq(ApCollection::getArticleId, dto.getEntryId()));

        ApArticle article = apArticleService.getById(dto.getEntryId());

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getEntryId());
        mess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
        if (collection == null && dto.getOperation() == 0) {
            ApCollection apCollection = new ApCollection();
            apCollection.setEntryId(id);
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setType(dto.getType());
            apCollection.setCollectionTime(new Date());
            apCollection.setPublishedTime(article.getPublishTime());
            //保存收藏
            save(apCollection);
            //收藏数
            mess.setAdd(1);
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
            return ResponseResult.okResult("收藏成功");
        } else if (article.getCollection()!=null&&dto.getOperation() == 1) {
            //取消收藏
            removeById(collection.getId());
            mess.setAdd(-1);
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
            return ResponseResult.okResult("取消收藏");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }


    }
